﻿using System;
#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif
using UnityEngine;

namespace JH
{
    public partial class AroundWithMoveCameraConfig
    {
        /// <summary>
        ///  旋转配置
        /// </summary>
        [Serializable]
        public class RotateConfig
        {
            /// <summary>
            ///  是否允许旋转
            /// </summary>
#if ODIN_INSPECTOR
            [LabelText("是否允许旋转")]
#endif
            public bool allowRotate = true;

            /// <summary>
            ///  旋转速度
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowRotate")] [LabelText("旋转速度")]
#endif
            public Vector2 rotateSpeed = new(16, 9);

            /// <summary>
            ///  旋转阻尼
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowRotate")] [LabelText("旋转阻尼")]
#endif
            public Vector2 rotateDamping = new(3, 3);

            /// <summary>
            ///  是否限制水平旋转
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowRotate")] [LabelText("限制水平旋转")]
#endif
            public bool limitHorizontal = false;

            /// <summary>
            ///  水平旋转限制
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowRotate")] [LabelText("限制水平旋转范围")] [EnableIf("limitHorizontal")]
#endif
            public Vector2 horizontalLimit = new(-180, 180);

            /// <summary>
            ///  垂直旋转限制，垂直旋转必然限制
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowRotate")] [LabelText("限制垂直旋转范围")]
#endif
            public Vector2 verticalLimit = new(-89.9f, 89.9f);

            /// <summary>
            ///  水平是否构成一个圈
            /// </summary>
            public bool IsHorizontalWrap()
            {
                var limit = GetHorizontalLimit();
                return limit.y - limit.x >= 360;
            }

            /// <summary>
            ///  获取垂直限制
            /// </summary>
            public Vector2 GetVerticalLimit()
            {
                return new Vector2(
                    Mathf.Min(verticalLimit.x, verticalLimit.y),
                    Mathf.Max(verticalLimit.x, verticalLimit.y));
            }

            /// <summary>
            /// 获取水平限制
            /// </summary>
            public Vector2 GetHorizontalLimit()
            {
                if (limitHorizontal)
                    return new Vector2(
                        Mathf.Min(horizontalLimit.x, horizontalLimit.y),
                        Mathf.Max(horizontalLimit.x, horizontalLimit.y));

                return new Vector2(-180, 180);
            }

            /// <summary>
            ///  获取水平限制后的值
            /// </summary>
            public float ClampHorizontal(float angle)
            {
                if (!limitHorizontal || IsHorizontalWrap()) return angle;

                var limit = GetHorizontalLimit();
                angle %= 360;

                return Mathf.Clamp(angle, limit.x, limit.y);
            }

            /// <summary>
            ///  获取垂直限制后的值
            /// </summary>
            public float ClampVertical(float angle)
            {
                var limit = GetVerticalLimit();
                angle %= 180;

                return Mathf.Clamp(angle, limit.x, limit.y);
            }
        }
    }
}